﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Globalization;
using Azad.BCL;
using System.Web.UI.HtmlControls;
using NazSoft.Framework;


/// <summary>
    /// Summary description for Extensions
    /// </summary>
public static class Extensions
{
    public static void AddMetaTags(this System.Web.UI.Page page, string name, string content)
    {
        // Set the keywords tag
        HtmlMeta metaTag = new HtmlMeta();
        metaTag.Name = name;
        metaTag.Content = string.IsNullOrEmpty(content) ? "news,newspaper,urdu,azad,daily,riasat,azad riasat,akhbar,taza tareen, karachi" : content;
        page.Header.Controls.Add(metaTag);
    }

    public static String ToXml<T>(this IList<T> list, String rootNodeName, String nodeName)
    {
        String finalString = String.Empty;
        StringBuilder s = new StringBuilder();
        s.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        s.Append("<" + rootNodeName + " >");
        foreach (var item in list)
        {
            s.Append(item.ToXml(nodeName));
        }

        s.Append("</" + rootNodeName + ">");

        finalString = s.ToString();

        return finalString;
    }
    public static String ToXml(this Object contentInfo, String nodeName)
    {
        Type t = contentInfo.GetType();
        String finalXml = "<" + nodeName + "";

        PropertyInfo[] pi = t.GetProperties();
        foreach (PropertyInfo p in pi)
        {

            if (p.PropertyType is IList)
            {
                ((IList)p).ToXml("dsD");
            }

            finalXml = finalXml + " " + p.Name + "=\"" + contentInfo.GetType().GetProperty(p.Name).GetValue(contentInfo, null) + "\"";
        }

        finalXml = finalXml + " />";

        return finalXml;
    }



    public static String Capitalize(this string text)
    {
        if (string.IsNullOrEmpty(text))
        {
            return string.Empty;
        }
        char[] a = text.ToCharArray();
        a[0] = char.ToUpper(a[0]);
        return new string(a);
    }

    public static String ZeroPad(this Int32 number)
    {
        return (number <= 9 ? "0" : "") + number;
    }

    public static Int16 ToInt16Safe(this String text)
    {
        Int16 finalValue = 0;
        Int16.TryParse(text, out finalValue);
        return finalValue;
    }

    public static Int32 ToInt32Safe(this String text)
    {
        Int32 finalValue = 0;
        Int32.TryParse(text, out finalValue);
        return finalValue;
    }

    public static Int64 ToInt64Safe(this String text)
    {
        Int64 finalValue = 0;
        Int64.TryParse(text, out finalValue);
        return finalValue;
    }

    public static DateTime ToDateTimeSafe(this String text)
    {
        DateTime dateTime = new DateTime();

        return text.ToDateTimeSafe(dateTime).Value;
    }

    public static DateTime? ToDateTimeSafe(this String text, DateTime? defaultValue)
    {
        DateTime dt = DatetimeUtility.GetCurrentDateTime();

        if (DateTime.TryParse(text, out dt))
        {
            defaultValue = dt;
        }

        return defaultValue;
    }

    public static Boolean IsNullOrEmpty<T>(this IList<T> list)
    {
        if (list == null)
        {
            return true;
        }

        if (list.Count <= 0)
        {
            return true;
        }

        return false;
    }
}
